package com.csun.cmny.provider.controller;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcNursingGroupNew;
import com.csun.cmny.provider.model.dto.OmcNursingGroupNewDto;
import com.csun.cmny.provider.pojo.Room;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcNursingGroupService;
import com.csun.cmny.provider.model.vo.OmcUserVo;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nursingGroup")
public class OmcNursingGroupController extends BaseController {

    @Autowired
    private OmcNursingGroupService omcNursingGroupServiceImpl;
    @Autowired
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 新增班组
      * @param nursingGroupNewDto
     * @return Wrapper
     * @author HD
     * @date 2021/11/10 14:15
     */
    /*@PostMapping("/insertNursingGroup")
    @Transactional
    public Wrapper insertNursingGroup(@RequestBody OmcNursingGroupNewDto nursingGroupNewDto){
        nursingGroupNewDto.setGroupId(getLoginAuthDto().getGroupId());
        OmcNursingGroupNewDto omcNursingGroupNewDto = new OmcNursingGroupNewDto();
        omcNursingGroupNewDto.setName(nursingGroupNewDto.getName());
        List<OmcNursingGroupNewDto> nursingGroupNewDtos = omcNursingGroupServiceImpl.selectNursingGroup(omcNursingGroupNewDto);
        if(nursingGroupNewDtos.size()>0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("10070003", null));
        }
        OmcNursingGroupNew omcNursingGroupNew = new OmcNursingGroupNew();
        BeanUtils.copyProperties(nursingGroupNewDto,omcNursingGroupNew);
        omcNursingGroupNew.setUpdateInfo(getLoginAuthDto());
        Integer result = omcNursingGroupServiceImpl.save(omcNursingGroupNew);
        List<OmcNursingGroupNewDto> select = omcNursingGroupServiceImpl.selectNursingGroup(nursingGroupNewDto);
        nursingGroupNewDto.setId(select.get(0).getId());
        *//**
          * 存储班组人员，房间信息
         *//*
        omcNursingGroupServiceImpl.saveOther(nursingGroupNewDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }*/

    /**
     * 查询班组
      * @param nursingGroupNewDto
     * @return null
     * @author HD
     * @date 2021/11/10 16:01
     */
    @PostMapping("/selectNursingGroup")
    public Wrapper selectNursingGroup(@RequestBody OmcNursingGroupNewDto nursingGroupNewDto){
        List<OmcNursingGroupNewDto> nursingGroupNewDtos = omcNursingGroupServiceImpl.selectNursingGroup(nursingGroupNewDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),nursingGroupNewDtos);
    }

    /**
     * 查询护工组详情
     */
    @PostMapping("/selectNuringUserInfo")
    public Wrapper selectNuringUserInfo(Long id,Integer pageNum,Integer pageSize){
        List<OmcUserVo> omcUserVos = omcNursingGroupServiceImpl.selectNuringUserInfo(id);
        PageHelper.startPage(pageNum,pageSize);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),new PageInfo<>(omcUserVos));
    }

    /**
     * 删除班组
     * @param nursingGroupNewDto
     * @return null
     * @author HD
     * @date 2021/11/10 16:01
     */
    /*@PostMapping("/deleteNursingGroup")
    public Wrapper deleteNursingGroup(@RequestBody OmcNursingGroupNewDto nursingGroupNewDto){
        Integer result = omcNursingGroupServiceImpl.deleteByKey(nursingGroupNewDto.getId());
        omcNursingGroupServiceImpl.deleteOther(nursingGroupNewDto.getId());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }*/
    /**
     * 修改班组
     * @param nursingGroupNewDto
     * @return null
     * @author HD
     * @date 2021/11/10 16:01
     */
    /*@Transactional
    @PostMapping("/updateNursingGroup")
    public Wrapper updateNursingGroup(@RequestBody OmcNursingGroupNewDto nursingGroupNewDto){
        nursingGroupNewDto.setGroupId(getLoginAuthDto().getGroupId());
        OmcNursingGroupNewDto omcNursingGroupNewDto = new OmcNursingGroupNewDto();
        omcNursingGroupNewDto.setName(nursingGroupNewDto.getName());
        List<OmcNursingGroupNewDto> nursingGroupNewDtos = omcNursingGroupServiceImpl.selectNursingGroup(omcNursingGroupNewDto);
        if(nursingGroupNewDtos.size() > 0 && !nursingGroupNewDtos.get(0).getId().equals(nursingGroupNewDto.getId())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("10070003", null));
        }
        OmcNursingGroupNew omcNursingGroupNew = new OmcNursingGroupNew();
        BeanUtils.copyProperties(nursingGroupNewDto,omcNursingGroupNew);
        Integer result = omcNursingGroupServiceImpl.update(omcNursingGroupNew);
        omcNursingGroupServiceImpl.deleteOther(omcNursingGroupNew.getId());
        *//**
         * 存储班组人员，房间信息
         *//*
        omcNursingGroupServiceImpl.saveOther(nursingGroupNewDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }*/

    /**
     * 查询班组负责的房间
     */
    @PostMapping("/queryRoom")
    public Wrapper selectRooms(Long nursingGroupId) {

        String msg_comment_query_param_error = cmnyMessageSource.getMessage("comment.query.param.error", null);
        String msg_comment_query_success = cmnyMessageSource.getMessage("comment.query.success", null);
        String msg_group_room_not_exist = cmnyMessageSource.getMessage("group.room.not.exist", null);

        if (null == nursingGroupId || nursingGroupId <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, msg_comment_query_param_error, null);
        }
        List<Room> list = omcNursingGroupServiceImpl.selectNursingBindRoom(nursingGroupId);
        if (CollectionUtils.isEmpty(list)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, msg_group_room_not_exist, null);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, msg_comment_query_success, list);
    }

    /**
     * 通过buildingId查询该机构没有被分配的房间
     */
    @RequestMapping(value = "/selectShowUnRoom", method = RequestMethod.POST)
    public Wrapper selectShowUnRoom(Long buildingId
    ) {
        String msg_group_select_bulid_param_error = cmnyMessageSource.getMessage("group.select.bulid.param.error", null);
        String msg_group_bulid_not_exist = cmnyMessageSource.getMessage("group.bulid.not.exist", null);
        String msg_comment_query_success = cmnyMessageSource.getMessage("comment.query.success", null);

        if (null == buildingId || buildingId <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, msg_group_select_bulid_param_error, null);
        }
        List<Room> list = omcNursingGroupServiceImpl.selectByBuildingId(buildingId);
        if (CollectionUtils.isEmpty(list)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, msg_group_bulid_not_exist, null);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, msg_comment_query_success, list);
    }

}
